package net.qhzw.admin.service.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.admin.entity.SysPermission;
import net.qhzw.admin.mapper.SysPermissionMapper;
import net.qhzw.admin.pojo.SysPermissionVo;
import net.qhzw.admin.service.SysPermissionService;
import net.qhzw.common.model.JsonResult;
import net.qhzw.common.util.StringUtils;
import net.qhzw.common.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SysPermissionServiceImpl implements SysPermissionService {


    @Autowired
    private SysPermissionMapper permissionMapper;

    @Override
    public JsonResult getPermission(String id) {
        SysPermission sysPermission = permissionMapper.selectByPrimaryKey(id);
        return JsonResult.success(sysPermission);
    }

    @Override
    public JsonResult getPermissionAll() {
        //全部数据
        List<SysPermissionVo> list = permissionMapper.selectPermissionAll();
        //根节点
        List<SysPermissionVo> rootNode = new ArrayList<>();
        for (SysPermissionVo permission : list) {
            if (permission.getPid() != null && "0".equals(permission.getPid())) {
                rootNode.add(permission);
            }
        }
        //获取根节点下的所有子节点
        for (SysPermissionVo permissionVo : rootNode) {
            List<SysPermissionVo> child = getChild(permissionVo.getId(), list);
            permissionVo.setChildren(child);
        }
        log.info("rootNode.size(): ==  [{}]", rootNode.size());

        return JsonResult.success(rootNode);
    }


    /***
     * 获取所有的子节点
     * @param id
     * @param list
     * @return
     */
    private List<SysPermissionVo> getChild(String id, List<SysPermissionVo> list) {
        //子节点
        List<SysPermissionVo> childList = new ArrayList<>();
        for (SysPermissionVo permissionVo : list) {
            if (id.equals(permissionVo.getPid())) {
                childList.add(permissionVo);
            }
        }

        for (SysPermissionVo permissionVo : childList) {
            List<SysPermissionVo> child = getChild(permissionVo.getId(), list);
            permissionVo.setChildren(child);
        }
        //如果节点下没有子节点，返回一个空List（递归退出）
        if (childList.size() == 0) {
            return new ArrayList<SysPermissionVo>();
        }
        return childList;
    }

    @Override
    public JsonResult edit(SysPermission permission) {
        if (permission.getId() == null || StringUtils.isEmpty(permission.getId())) {
            //添加操作
            permission.setId(UUIDUtil.getUUid());
            permission.setCreateTime(new Date());
            int rwo = permissionMapper.insertSelective(permission);
            return JsonResult.result(rwo, "添加失败");
        }
        int rwo = permissionMapper.updateByPrimaryKeySelective(permission);
        return JsonResult.result(rwo, "修改失败");
    }

    @Override
    public JsonResult deletePermission(String id) {
        SysPermissionVo permissionVo = permissionMapper.selectPermissionVoById(id);
        if (permissionVo != null) {
            return JsonResult.error("该目录下有子级，请先删除子级！");
        }
        return JsonResult.success();
//        return JsonResult.result(row, "删除失败");
    }


}
